﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;
using Ext.Net;
using Ext.Net.MVC;
using System.Linq.Dynamic;

namespace DoAnWebsite.Controllers
{
    public class GroupVideoController : Controller
    {
        //Video
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();

        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult ListGVideo()
        {
            Session["includeInActiveGVideo"] = false;
            Session["CharFilterGVideo"] = "";
            return View();
        }

        public ActionResult IncludeInActive()
        {
            var store = X.GetCmp<Store>("GVideoStore");

            Session["includeInActiveGVideo"] = !(bool)Session["includeInActiveGVideo"];

            store.Reload();
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult CharFilterGVideo(string filterstring)
        {
            var store = X.GetCmp<Store>("GVideoStore");
            Session["CharFilterGVideo"] = filterstring;
            store.Reload();
            return this.Direct();
        }

        public ActionResult BindingGVideoStore(StoreRequestParameters parameters)
        {
            var sampleData = db.GroupVideos.Select(m => new
            {
                m.GroupVideoId,
                m.GVName,
                m.Title,
                m.Description,
                m.Keyword,
                m.Active
            });


            bool includeInActive = (bool)Session["includeInActiveGVideo"];
            string charFilter = (string)Session["CharFilterGVideo"];

            var result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active)));
            if ("0".Equals(charFilter))
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.Active) &&
                    (ent.GVName.StartsWith("0") || ent.GVName.StartsWith("1") ||
                    ent.GVName.StartsWith("2") || ent.GVName.StartsWith("3") ||
                    ent.GVName.StartsWith("4") || ent.GVName.StartsWith("5") ||
                    ent.GVName.StartsWith("6") || ent.GVName.StartsWith("7") ||
                    ent.GVName.StartsWith("8") || ent.GVName.StartsWith("9")
                    )));
            }

            else
            {
                result = sampleData.Where(ent => ((includeInActive || ent.Active) && ent.GVName.StartsWith(charFilter)));

            }

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.GroupVideoId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData.Select(ent => new { ent.GroupVideoId, ent.GVName, ent.Title, ent.Description, ent.Keyword, ent.Active }), result.Count());
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult CreateGVideo()
        {
            ViewBag.HideTextBox = false;

            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateGVideo(GroupVideo model)
        {
            if (ModelState.IsValid)
            {
                model.Active = true;
                db.GroupVideos.AddObject(model);
                db.SaveChanges();
                X.Msg.Alert("Sucess", "Create GroupVideo success!").Show();
                return this.FormPanel();
            }

            X.Msg.Alert("Non Sucess", "Create GroupVideo non success!").Show();

            return this.FormPanel();
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditGVideo(int id)
        {
            ViewBag.HideTextBox = false;
            GroupVideo groupVideo = db.GroupVideos.Single(m => m.GroupVideoId == id);

            return View(groupVideo);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditGVideo(GroupVideo model)
        {
            GroupVideo groupVideo1 = db.GroupVideos.Single(c => c.GroupVideoId == model.GroupVideoId);
            if (ModelState.IsValid)
            {
                db.GroupVideos.Detach(groupVideo1);
                db.GroupVideos.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                X.Msg.Alert("Sucess", "Edit GroupVideo success!").Show();
                return this.FormPanel();
            }
            X.Msg.Alert("Non Sucess", "Edit GroupVideo non success!").Show();
            return this.FormPanel();
        }

    }
}
